import { computed } from 'vue';
import { smartTranslate as $t } from '@/lang';

export const useColumns = (options = {}) => {
  const {
    fieldColumns = [],
    actionButtons = [],
    isSelectionMode,
    isCheckboxMode,
    selectType,
  } = options;

  // 默认字段列配置
  const defaultFieldColumns = computed(() => [...fieldColumns.value]);

  // 默认操作按钮配置
  const defaultActionButtons = [...actionButtons];

  // 操作列配置
  const actionColumn = computed(() => [
    {
      field: 'operation',
      title: $t('common.field.operation', '操作栏'),
      width: 'auto',
      align: 'center',
      fixed: 'right',
      visible: true,
      cellRender: {
        name: 'RaElButtonSetRender',
        buttons: defaultActionButtons,
      },
    },
  ]);

  // 合并列配置
  const columns = computed(() => {
    let type = selectType.value;

    if (isSelectionMode.value && !isCheckboxMode.value) {
      type = 'radio';
    }

    const selectionColumn = [
      {
        title: '#',
        field: 'selection',
        type: type,
        width: 50,
        align: 'center',
      },
    ];

    let columnConfig = [];
    if (isSelectionMode.value) {
      if (type === 'none') {
        columnConfig = [...defaultFieldColumns.value];
      } else {
        columnConfig = [...selectionColumn, ...defaultFieldColumns.value];
      }
    } else {
      if (type === 'none') {
        columnConfig = [...defaultFieldColumns.value, ...actionColumn.value];
      } else {
        columnConfig = [
          ...selectionColumn,
          ...defaultFieldColumns.value,
          ...actionColumn.value,
        ];
      }
    }
    return columnConfig;
  });

  return { columns };
};
